using System;
using System.Collections.Generic;
using UnityEngine;

[Serializable]
public class Achievement
{
	public string achievementNameID;

	public int sortIndex;

	public string achievementIcon;

	public int neededCount = 1;

	public Reward reward;

	public int currentCount;

	public bool achieved;

	public bool rewarded;

	public bool canGiveReward;

	public bool Increment(int value = 1, bool unlockIfAchieved = true)
	{
		bool result = false;
		if (settings.learningCompleted)
		{
			IncrementCounter(value);
			if (unlockIfAchieved && currentCount >= neededCount)
			{
				if (!rewarded)
				{
					canGiveReward = true;
				}
				if (!achieved)
				{
					UnlockAchivment();
					SaveAchieved(true);
					result = true;
				}
			}
			SaveAchievementState();
		}
		return result;
	}

	public virtual void Init()
	{
		currentCount = BCWStorage.Get<int>(achievementNameID + "_count");
		achieved = BCWStorage.Get<bool>(achievementNameID + "_achieved");
		rewarded = BCWStorage.Get<bool>(achievementNameID + "_rewarded");
		achievementIcon = achievementNameID;
		if (currentCount < neededCount)
		{
			if (achieved)
			{
				SaveAchieved(false);
			}
		}
		else
		{
			if (!rewarded)
			{
				canGiveReward = true;
			}
			else
			{
				switch (reward.rewardGroup)
				{
				case RewardGroups.Product:
					if (!MarketController.Instance.GetProduct(reward.rewerdID).Purchased)
					{
						canGiveReward = true;
					}
					break;
				}
			}
			if (!achieved)
			{
				UnlockAchivment();
				SaveAchieved(true);
			}
		}
		if (!AchievementsManager.instanse.allAchievements.Contains(this))
		{
			AchievementsManager.instanse.allAchievements.Add(this);
		}
	}

	public void SaveAchievementState()
	{
		if (currentCount != 0)
		{
			BCWStorage.Set(achievementNameID + "_count", currentCount);
		}
		if (achieved)
		{
			BCWStorage.Set(achievementNameID + "_achieved", achieved, true);
		}
		if (rewarded)
		{
			BCWStorage.Set(achievementNameID + "_rewarded", rewarded, true);
		}
	}

	public virtual bool CheckAchievement(KillInfo killInfo)
	{
		BCWDebug.Log("****************** Check Achievement Base ******************");
		return true;
	}

	private void IncrementCounter(int value)
	{
		if (!achieved)
		{
			currentCount += value;
		}
	}

	private void SaveAchieved(bool value)
	{
		achieved = value;
	}

	public void SaveRewarded(bool value)
	{
		rewarded = value;
	}

	public void Unset()
	{
		BCWDebug.Log("Unset achievement - " + achievementNameID);
		BCWStorage.Set(achievementNameID + "_count", 0);
		BCWStorage.Set(achievementNameID + "_achieved", false);
		BCWStorage.Set(achievementNameID + "_rewarded", false);
	}

	public virtual void UnlockAchivment()
	{
		BCWDebug.Log("***** Achievement " + achievementNameID + " UNLOCKED! *****");
		Dictionary<string, string> dictionary = new Dictionary<string, string>();
		dictionary.Add("Achievment_Complete", achievementNameID);
		FlurryWrapper.LogEvent(FlurryWrapper.EV_ACHIVEMENTS_COMPLETE, dictionary);
		AchievementsManager.instanse.ShowAchievementUnlokedPanel(this);
	}

	public virtual void GiveReward(Vector3 position)
	{
		BCWDebug.Log("***** Achievement " + achievementNameID + " REWARDED! *****");
		AchievementsManager.instanse.GiveAchievementReward(reward, position);
		SaveAchievementState();
		canGiveReward = false;
	}
}
